Trò chơi 2D Cow Boy Runner

52.955 lượt xem;
1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.UI;
5 using
UnityEngine.SceneManagement;
6
7 public
class GameManagerScript : MonoBehaviour {
8
9     
public static GameManagerScript instance;
10
11     
[SerializeField]
12     
public GameObject pausePanel, gameOverPanel;
13
14     
[SerializeField]
15     
public Button pauseButton, resumeButton, playAgainButton, gotoMenuButton;
16
17     
public Text scoreText, coinScore, bestScore, highCoinScore, gameOverBestScore, gameOverHighCoinScore;
18
19     
[SerializeField]
20     
public Text newHighScoreText, newHighCoinText;
21
22     
[SerializeField]
23     
public Image highScoresImage;
24
25     
public AudioSource audioSource;
26     
public AudioClip cheerClip;
27
28     
public Transform groundGenerator;
29     
private Vector3 groundStartPoint;
30
31     
public PlayerMoveScript player;
32     
private Vector3 playerStartPoint;
33
34     
void Start () {
35         groundStartPoint = groundGenerator.position;
36         playerStartPoint = player.transform.position;
37     }
38     
39     
// Update is called once per frame
40     
void Update () {
41         
42     }
43
44     
public void RestartGame () {
45         StartCoroutine (
"RestartGameCoroutine");
46     }
47
48     
public IEnumerator RestartGameCoroutine() {
49         player.gameObject.SetActive (
false);
50         
yield return new WaitForSeconds (0.5f);
51         player.transform.position = playerStartPoint;
52         groundGenerator.position = groundStartPoint;
53         player.gameObject.SetActive (
true);
54     }
55
56     
public void pauseGame () {
57         pausePanel.SetActive (
true);
58         bestScore.text =
"Best Score: " + PlayerPrefs.GetInt ("bestScore");
59         highCoinScore.text =
"Best Coin Score: " +PlayerPrefs.GetInt ("bestCoinScore");
60         Time.timeScale =
0f;
61     }
62
63     
public void resumeGame () {
64         pausePanel.SetActive (
false);
65         scoreText.text =
"Score: " + PlayerMoveScript.instance.scoreCount;
66         Time.timeScale =
1f;
67     }
68
69     
public void playAgain () {
70         Time.timeScale =
1f;
71         pauseButton.gameObject.SetActive (
true);
72         SceneManager.LoadScene (
"Gameplay");
73         gameOverPanel.SetActive (
false);
74     }
75
76     
public void gotoMenu () {
77         SceneManager.LoadScene (
"Main");
78     }
79
80     
public void SetCoinScore (int score) {
81         coinScore.text =
"Coin: " + score;
82     }
83
84     
public void SetScore () {
85         scoreText.text =
"Score: " + PlayerMoveScript.instance.scoreCount;
86     }
87
88     
public void gameOver (int score, int coins) {
89         coinScore.gameObject.SetActive (
false);
90         pauseButton.gameObject.SetActive (
false);
91         gameOverPanel.SetActive (
true);
92         gameOverBestScore.text =
"Your Score: " + score;
93         gameOverHighCoinScore.text =
"Your Coin Score: " + coins;
94
95     }
96
97     
public void ifPlayerDiedScore(int score) {
98         bestScore.text =
"Best Score: " + PlayerMoveScript.instance.scoreCount;
99
100         
if (score > Scores.instance.GetHighScore ()) {
101             Scores.instance.SetHighScore (score);
102             newHighScoreText.gameObject.SetActive (
true);
103             highScoresImage.gameObject.SetActive (
true);
104             audioSource.PlayOneShot (cheerClip);
105             Debug.Log (
"New High Score");
106         }
107
108         bestScore.text =
"Best Score: " + Scores.instance.GetHighScore ();
109     }
110
111     
public void ifPlayerDiedCoinScore(int score) {
112         highCoinScore.text =
"Best Coin Score: " + coinScore;
113
114         
if (score > Scores.instance.GetHighCoinScore ()) {
115             Scores.instance.SetHighCoinScore (score);
116             newHighCoinText.gameObject.SetActive (
true);
117             highScoresImage.gameObject.SetActive (
true);
118             audioSource.PlayOneShot (cheerClip);
119         }
120
121         highCoinScore.text =
"Best Coin Score: " + Scores.instance.GetHighCoinScore ();
122     }
123         
124 }


Update is called once per frame



Gõ tìm kiếm nhanh...